//
//  RecommendAppScrollView.m
//  RecommondProduct
//
//  Created by Haibo Tang on 11-10-17.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "RecommendAppScrollView.h"
#import "Cell.h"

#define kWidth 100.f
#define kHeight 100.f
#define kPaddingLeft 10.f
#define kPaddingTop 30.f

#define kFullWidth (320.f)

#define kTagBase 5600
#define kCount ((int) ((kFullWidth+kPaddingLeft)/(kHeight+kPaddingLeft)))
@interface RecommendAppScrollView (private)
- (void)setupView;

- (void)loadCellAtIndex:(int)index;
- (void)unloadCellAtIndex:(int)index;
@end

@implementation RecommendAppScrollView
@synthesize dataSourceMArray = _dataSourceMArray;

- (id)initWithDataSource:(NSMutableArray*)datasource{
    self = [self initWithFrame:CGRectMake(0.f, 0.f, 320.f, 100.f)];
    if (self) {
        [self setDataSource:datasource];
    }
    return self;
}

- (void)setDataSource:(NSMutableArray *)datasource{
    self.dataSourceMArray = datasource;
    int count = [self.dataSourceMArray count];
    if (count>0) {
        float width = count*(kHeight+kPaddingLeft)-kPaddingLeft;
        if (width<320.f) {
            width = 320.f;
        }
        self.contentSize = CGSizeMake(width, kHeight);
        [self setupView];
        
        self.delegate = self;
    }
}

- (void)setupView{
    Cell *cell = nil;
    int i=0;
    for (NSDictionary *dict in self.dataSourceMArray) {
        cell = [[[Cell alloc] initWithFrame:CGRectMake((kWidth+kPaddingLeft)*i, 0.f, kWidth, kHeight)] autorelease];
        cell.backgroundColor = [UIColor yellowColor];
        [self addSubview:cell];
        [self bringSubviewToFront:cell];
        cell.tag = i+kTagBase;
        
        i++;
        //初始化的时候只需加载到屏幕外下一个
        if (i>kCount) {
            break;
        }
    }
    
    _left = 0;
    _right = i-1;
}
- (void)dealloc{
    self.dataSourceMArray = nil;
    
    [super dealloc];
}

#pragma mark - private function
- (void)loadCellAtIndex:(int)index{
    if (index>=[self.dataSourceMArray count]) {
        return;
    }
    UIView *cell = [self viewWithTag:(index+kTagBase)];
    if (cell!=nil) {
        return;
    }
    cell = [[[Cell alloc] initWithFrame:CGRectMake((kWidth+kPaddingLeft)*index, 0.f, kWidth, kHeight)] autorelease];
    cell.backgroundColor = [UIColor redColor];
    cell.tag = index+kTagBase;
    [self addSubview:cell];
    NSLog(@"加载%d", index);
 
}

- (void)unloadCellAtIndex:(int)index{
    if (index<0) {
        return;
    }
    
    UIView *cell = [self viewWithTag:(index+kTagBase)];
    //如果cell不存在，不需要操作
    if (cell==nil) {
        return;
    }
    NSLog(@"卸载%d;%@", index, cell);
    [cell removeFromSuperview];
    
}
#pragma mark - scrollView delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _startPoint = scrollView.contentOffset;
}

#define kSpeed 50.f

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float offsetX = scrollView.contentOffset.x;
    
    //超出两边不做处理
    if (offsetX>([self.dataSourceMArray count]*(kWidth+kPaddingLeft)-kPaddingLeft-kFullWidth) || offsetX<0) {
        return;
    }
    
    int count = (int)(offsetX/(kWidth+kPaddingLeft));
    if ((offsetX-_startPoint.x)>0) {
        NSLog(@"<-----%d", _right);
        int loadTag = count+1+kCount;
        if (loadTag!=_loadedTag && offsetX>(kWidth*0.5f+count*(kWidth+kPaddingLeft))) {
            _loadedTag = loadTag;
            for (int i=_right+1; i<=loadTag; i++) {
                [self loadCellAtIndex:i];
                [self unloadCellAtIndex:i-(kCount+2)];
            }
            
            _right = _loadedTag;
            if (_right>=[self.dataSourceMArray count]) {
                _right = [self.dataSourceMArray count]-1;
            }
            if (_right<kCount) {
                _right = kCount;
            }
        }
    }else{
        NSLog(@"%d----->", _right-kCount);
        int loadTag = count-1;
        if (loadTag!=_loadedTag && offsetX<(kWidth*0.5f+count*(kWidth+kPaddingLeft))) {
            _loadedTag = loadTag;
            for (int i=loadTag; i<=(_right-kCount-1); i++) {
                [self loadCellAtIndex:i];
                [self unloadCellAtIndex:i+(kCount+2)];
            }
            
            _right = loadTag+kCount+1;
            if (_right>=[self.dataSourceMArray count]) {
                _right = [self.dataSourceMArray count]-1;
            }
            if (_right<kCount) {
                _right = kCount;
            }
        }
    }
}

#pragma mark - public function
- (void)scrollWithLength:(float)length{
    CGPoint pt = self.contentOffset;
    pt.x += length;
    //如果到了边界
    if (pt.x<0) {
        pt.x = 0;
    }else if(pt.x>(self.contentSize.width-kFullWidth)){
        pt.x = self.contentSize.width-kFullWidth;
        if (pt.x<0) {
            pt.x=0;
        }
    }
    _startPoint.x = self.contentOffset.x;
    [self setContentOffset:pt animated:YES];
}
- (void)scrollToRight{
    float length = kWidth+kPaddingLeft;
    [self scrollWithLength:length];
}
- (void)scrollToLeft{
    float length = kWidth+kPaddingLeft;
    [self scrollWithLength:-length]; 
}

@end
